package com.xzq.daoimpl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.xzq.dao.ProductDao;
import com.xzq.entity.Page;
import com.xzq.entity.Product;
import com.xzq.entity.Type;
import com.xzq.entity.page.PageSize;
import com.xzq.util.HibernatePage;
@Repository
public class ProductDaoImpl extends HibernatePage<Product> implements ProductDao {

	@Override
	public Page findAllProduct(Page page, Integer choose) {
		String hql = "from Product p ";
		switch (choose) {
		case 1:
			hql = hql+"order by p.price";
			break;
		case 2:
			hql = hql+"order by p.price desc";
			break;
		case 3:
			hql = hql+"order by p.addDate";
			break;
		case 4:
			hql = hql+"order by p.addDate desc";
			break;
		default:
			break;
		}
		page = findHibernatePage(page, hql, null, PageSize.productSize);
		return page;
	}

	@Override
	public Page findAllProductByType(Page page, Type type, Integer choose) {
		String hql = "from Product p where p.type.id = ? ";
		switch (choose) {
		case 1:
			hql = hql+"order by p.price";
			break;
		case 2:
			hql = hql+"order by p.price desc";
			break;
		case 3:
			hql = hql+"order by p.addDate";
			break;
		case 4:
			hql = hql+"order by p.addDate desc";
			break;
		default:
			break;
		}
		Object[] obj = {type.getId()};
		page = findHibernatePage(page, hql, obj, PageSize.productSize);
		return page;
	}

	@Override
	public Page findLikeNameProduct(Page page, String likename, Integer choose) {
		String hql = "from Product p where name like ? ";
		switch (choose) {
		case 1:
			hql = hql+"order by p.price";
			break;
		case 2:
			hql = hql+"order by p.price desc";
			break;
		case 3:
			hql = hql+"order by p.addDate";
			break;
		case 4:
			hql = hql+"order by p.addDate desc";
			break;
		default:
			break;
		}
		Object[] obj = {"%"+likename+"%"};
		page = findHibernatePage(page, hql, obj, PageSize.productSize);
		return page;
	}

	@Override
	public Product findOneProduct(Product product) {
		product = template.get(Product.class, product.getId());
		return product;
	}

	@Override
	public int addProduct(Product product) {
		Serializable s = template.save(product);
		int count = (Integer) s;
		return count;
	}

	@Override
	public int modifyProduct(Product product) {
		try {
			template.update(product);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public int deleteProduct(Product product) {
		try {
			template.delete(product);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}



}
